package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.HotSetmeal;
import com.itheima.health.pojo.Report;
import com.itheima.health.service.ReportService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;


/**
 * 获取运营数据统计
 */
@Slf4j
@RestController
@RequestMapping("/report")
public class ReportController {
    @Autowired
    private ReportService reportService;

    /**
     * 获取运营数据
     *
     * @return
     */
    @GetMapping("/getBusinessReportData")
    public Result getBusinessReportData() {
        return new Result(true, MessageConst.GET_BUSINESS_REPORT_SUCCESS, reportService.getBusinessReportData());
    }

    /**
     * 导出运营数据报表
     *
     * @return
     */
 /*   1. 调用Service，获取统计数据
      2. 获得报表日期
3. 获取与会员有关数据 (今日、本周、本月、总人数)
4. 获取与预约有关数据 (今日、本周、本月)
5. 获得到诊相关数据 (今日、本周、本月)
6. 获得套餐热门数据
7. 构造excel文件并返回给前端
   1. 利用流，读Excel文件，构建Workbook对象，获取工作表
   2. 获取报表日期行
   3. 获取新增会员及会员总数行
   4. 获取本周新增会员及本月新增会员行
   5. 获取今日预约及到诊数行
   6. 获取本月预约及本月到诊行
   7. 获取热门套餐行，动态写入数据
   8. 通过输出流进行文件下载*/
    @GetMapping("exportBusinessReport")
    public Result exportBusinessReport(HttpServletResponse response) {
        log.info("[导出运营数据报表]开始");
        //读取模板构造workBook
        Report businessReportData = reportService.getBusinessReportData();
        String reportDate = businessReportData.getReportDate();
        int todayNewMember = businessReportData.getTodayNewMember();
        int totalMember = businessReportData.getTotalMember();
        int thisWeekNewMember = businessReportData.getThisWeekNewMember();
        int thisMonthNewMember = businessReportData.getThisMonthNewMember();
        int todayOrderNumber = businessReportData.getTodayOrderNumber();
        int todayVisitsNumber = businessReportData.getTodayVisitsNumber();
        int thisWeekOrderNumber = businessReportData.getThisWeekOrderNumber();
        int thisWeekVisitsNumber = businessReportData.getThisWeekVisitsNumber();
        int thisMonthOrderNumber = businessReportData.getThisMonthOrderNumber();
        int thisMonthVisitsNumber = businessReportData.getThisMonthVisitsNumber();
        List<HotSetmeal> hotSetmeal = businessReportData.getHotSetmeal();
        try (InputStream is = this.getClass().getClassLoader().getResourceAsStream("report_template.xlsx");
             Workbook workbook = new XSSFWorkbook(is);

             OutputStream os = response.getOutputStream()) {
            Sheet sheet = workbook.getSheetAt(0);
            //获取sheet中的第n行，第m个单元格，设置单元格的值,n m为正整数
            sheet.getRow(4).getCell(6).setCellValue(reportDate);
            sheet.getRow(6).getCell(6).setCellValue(todayNewMember);
            sheet.getRow(6).getCell(8).setCellValue(totalMember);
            sheet.getRow(7).getCell(6).setCellValue(thisWeekNewMember);
            sheet.getRow(7).getCell(8).setCellValue(thisMonthNewMember);
            sheet.getRow(9).getCell(6).setCellValue(todayOrderNumber);
            sheet.getRow(9).getCell(8).setCellValue(todayVisitsNumber);
            sheet.getRow(10).getCell(6).setCellValue(thisWeekOrderNumber);
            sheet.getRow(10).getCell(8).setCellValue(thisWeekVisitsNumber);
            sheet.getRow(11).getCell(6).setCellValue(thisMonthOrderNumber);
            sheet.getRow(11).getCell(8).setCellValue(thisMonthVisitsNumber);

            for (int i = 0; i < hotSetmeal.size(); i++) {
                HotSetmeal hotSetmeal1 = hotSetmeal.get(i);
                String name = hotSetmeal1.getName();
                int setmeal_count = hotSetmeal1.getSetmeal_count();
                Double proportion = hotSetmeal1.getProportion();
                sheet.getRow(i+14).getCell(5).setCellValue(name);
                sheet.getRow(i+14).getCell(6).setCellValue(setmeal_count);
                sheet.getRow(i+14).getCell(7).setCellValue(proportion);
            }
            //写入网络输出流
            //写入返回流
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("content-Disposition", "attachment;fileName=" + reportDate + "_report.xlsx");
            workbook.write(os);
            return null;
        } catch (IOException e) {
            log.error("", e);
            return new Result(false, MessageConst.ACTION_FAIL);
        }
    }

    /**
     * 展示过去12个月时间内的每个月的会员总数据量。
     */

    @GetMapping("/getMemberReport")
    public Result getMemberReport(){
        return new Result(true, MessageConst.GET_MEMBER_NUMBER_REPORT_SUCCESS,reportService.getMemberReport());
    }
    @GetMapping("/getSetmealReport")
    public Result getSetmealReport(){
        return new Result(true, MessageConst.GET_SETMEAL_COUNT_REPORT_SUCCESS,reportService.getSetmealReport());
    }
}
